package com.taoz.mianshiwa.commentservice.controller;

import cn.hutool.core.bean.BeanUtil;
import com.taoz.mianshiwa.commentservice.model.domain.Comment;
import com.taoz.mianshiwa.commentservice.model.request.CommentAddDTO;
import com.taoz.mianshiwa.commentservice.resp.BaseResponse;
import com.taoz.mianshiwa.commentservice.resp.ResultUtils;
import com.taoz.mianshiwa.commentservice.resp.exception.ErrorCode;
import com.taoz.mianshiwa.commentservice.resp.exception.ThrowUtils;
import com.taoz.mianshiwa.commentservice.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addComment(@RequestBody CommentAddDTO commentAddDTO) {
        ThrowUtils.throwIf(!commentAddDTO.checkValidate(), ErrorCode.PARAMS_ERROR);
        commentService.save(BeanUtil.copyProperties(commentAddDTO, Comment.class));
        return ResultUtils.success(true);
    }

    @GetMapping("/delete")
    public BaseResponse<Boolean> deleteCommentById(@RequestParam("id") Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(commentService.removeById(id));
    }

    @GetMapping
    public BaseResponse<Comment> getCommentById(@RequestParam("id") Long id) {
        return ResultUtils.success(commentService.getById(id));
    }
}
